package io.cici.cc.mybatis.plus.core.injector.methods;

import io.cici.cc.mybatis.lite.mapping.MappedStatement;
import io.cici.cc.mybatis.lite.mapping.SqlSource;
import io.cici.cc.mybatis.plus.core.enums.SqlMethod;
import io.cici.cc.mybatis.plus.core.injector.Method;
import io.cici.cc.mybatis.plus.core.metadata.TableInfo;

/**
 * 根据ID 查询一条数据
 */
public class SelectById extends Method {

    public SelectById() {
        this(SqlMethod.SELECT_BY_ID.getMethod());
    }

    /**
     * @param name 方法名
     * @since 3.5.0
     */
    public SelectById(String name) {
        super(name);
    }

    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        SqlMethod sqlMethod = SqlMethod.SELECT_BY_ID;
        SqlSource sqlSource = super.createSqlSource(configuration,
                String.format(sqlMethod.getSql(), sqlSelectColumns(tableInfo, false), tableInfo.getTableName(),
                        tableInfo.getKeyColumn(), tableInfo.getKeyProperty()),
                Object.class);
        return this.addSelectMappedStatementForTable(mapperClass, methodName, sqlSource, tableInfo);
    }
}
